<template>
  <div class="login-container">
    <div><span style="float: right;color: #ffffff;cursor: pointer" class="m-10" @click="logOut">退出登录</span></div>
    <el-form ref="loginForm" :model="loginForm" label-width="80px" class="login-form" auto-complete="on" label-position="left">
      <h3 class="title">商户选择</h3>
      <el-form-item label="商户选择" >
        <el-select v-model="bussiness" placeholder="请选择" style="width: 100%">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            sortable/>
        </el-select>
      </el-form-item>
      <div style="display: flex; justify-content: space-around">
        <el-button v-if="user_level" type="primary" style="width:30%;" @click.native.prevent="handleLogin">商户管理</el-button>
        <el-button v-if="isWorkgo" type="primary" style="width:30%" @click="goWork"> 业务平台</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { getUserPreset } from '@/api/business'
import { mapMutations } from 'vuex'
import { removeCookies, MenuList } from '@/utils/auth'
export default {
  name: 'Login',
  data() {
    return {
      data: {},
      user_level: '',
      bussiness: '',
      options: [],
      roleLevel: 2,
      roleObj: {},
      loginForm: {
        nickname: '',
        email: '',
        password: '',
        compassword: ''
      }
    }
  },
  computed: {
    isWorkgo() {
      return this.bussiness
    }
  },
  created() {
    removeCookies(MenuList)
    this.SET_menuList([])
    this.getData()
  },
  methods: {
    ...mapMutations(['SET_roleSelf', 'SET_BUSINESS', 'SET_menuList', 'SET_ISBACKCHOOSE']),
    getData() {
      getUserPreset({}).then((res) => {
        this.data = res.data
        this.user_level = res.data.is_root
        this.options = res.data.list.map(item => {
          this.roleObj[item.merchant_name] = item.merchant_label
          return { label: item.merchant_label, value: item.merchant_name, role_level: item.role_level }
        })
        if (this.options.length > 0) {
          this.bussiness = this.options[0].value
        }
        if (this.options.length === 1 && (!this.user_level)) {
          this.SET_ISBACKCHOOSE(false)
          this.goWork()
        } else {
          this.SET_ISBACKCHOOSE(true)
        }

        if (this.options.length === 0) {
          this.$message({ type: 'error', message: '您没有商户权限，请联系管理员' })
        }
      }, err => {
        this.$message({ type: 'error', message: err.response.data.msg })
      })
    },
    goWork() {
      this.SET_roleSelf('bussiness')
      this.options.forEach(item => {
        if (this.bussiness === item.value) {
          this.roleLevel = item.role_level
        }
      })
      this.SET_BUSINESS({ businessName: this.roleObj[this.bussiness], business: this.bussiness, roleLevel: this.roleLevel })
      this.$router.push({
        path: '/dashboard'
      })
    },
    logOut() {
      this.$store.dispatch('LogOut').then(() => {
        location.reload() // 为了重新实例化vue-router对象 避免bug
      })
    },
    handleLogin() {
      this.SET_roleSelf('master')
      this.SET_BUSINESS('')
      this.$router.push({
        path: '/merchant'
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg:#2d3a4b;
  $light_gray:#eee;

  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: #fff !important;
        }
      }
    }
    .el-form-item__content {
      /*border: 1px solid rgba(255, 255, 255, 0.1);*/
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    .el-form-item__label{
      color: #ffffff;
    }
  }

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;
  .login-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: $bg;
    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 520px;
      max-width: 100%;
      padding: 35px 35px 15px 35px;
      margin: 120px auto;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
</style>
